//
//  JPHttpManagerClass.m
//  JPShop
//
//  Created by 虎爷 on 2019/9/27.
//  Copyright © 2019 虎爷. All rights reserved.
//

#import "GPRequsetManager.h"

@implementation GPRequsetManager

-(instancetype)init{
    return [GPRequsetManager manager];
}

+(instancetype)HttpRequstManager{
    GPRequsetManager *manager = [GPRequsetManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/plain",nil];
    
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];

    
    manager.requestSerializer.timeoutInterval = 10.0f;
    manager.securityPolicy = [AFSecurityPolicy defaultPolicy];
    manager.securityPolicy.allowInvalidCertificates = YES;
    manager.securityPolicy.validatesDomainName = NO;
    
    return manager;
}




-(void)PostURL:(NSString *)appendURl
        params:(NSDictionary *)params
       success:(successBlock)sucess
          fail:(failBlock)fail{
    
    
    NSMutableDictionary *paramsDic;
    if ([params isKindOfClass:[NSNull class]] || [params isEqual:[NSNull null]]) {
        paramsDic = [[NSMutableDictionary alloc]init];
    }else{
        paramsDic = [NSMutableDictionary dictionaryWithDictionary:params];
    }

    paramsDic[@"productId"] = PRODUCT_ID;
    
    [[JPAccount sharedJPAccount]LoadTokenFromSandbox];
    NSString *token = [JPAccount sharedJPAccount].token;
    paramsDic[@"outOpenId"] = token;
    paramsDic[@"os"] = [NSNumber numberWithInt:1];

    [self POST:appendURl parameters:paramsDic progress:^(NSProgress * _Nonnull uploadProgress) {
        NSLog(@"%@",appendURl);
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSInteger statusNum = [responseObject[@"code"] integerValue];

        if (sucess) {
            sucess(statusNum, responseObject);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [SVProgressHUD showErrorWithStatus:@"网络错误，请检查网络"];
        NSLog(@"网络错误地址=%@",appendURl);
        if (fail) {
            fail(error);
        }
    }];
}


-(void)PostURL2:(NSString *)appendURl
        params:(NSDictionary *)params
       success:(successBlock)sucess
          fail:(failBlock)fail{
    
    
    NSMutableDictionary *paramsDic = [[NSMutableDictionary alloc]init];
   
    NSMutableDictionary *bDic = [[NSMutableDictionary alloc]init];
    bDic[@"productId"] = PRODUCT_ID;
    [[JPAccount sharedJPAccount]LoadTokenFromSandbox];
    NSString *token = [JPAccount sharedJPAccount].token;
    bDic[@"token"] = token;
    bDic[@"os"] = [NSNumber numberWithInt:1];
    bDic[@"version"] = VERSION;
    bDic[@"channelId"] = [NSNumber numberWithInt:0];
    
    paramsDic[@"b"] = bDic;
    
    NSMutableDictionary *oDic;
    if ([params isKindOfClass:[NSNull class]] || [params isEqual:[NSNull null]]) {
        oDic = [[NSMutableDictionary alloc]init];
    }else{
        oDic = [NSMutableDictionary dictionaryWithDictionary:params];
    }
    paramsDic[@"o"] = oDic;
    

    [self POST:appendURl parameters:paramsDic progress:^(NSProgress * _Nonnull uploadProgress) {
        NSLog(@"%@",appendURl);
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSInteger statusNum = [responseObject[@"code"] integerValue];

        if (sucess) {
            sucess(statusNum, responseObject);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [SVProgressHUD showErrorWithStatus:@"网络错误，请检查网络"];
        NSLog(@"网络错误地址=%@",appendURl);
        if (fail) {
            fail(error);
        }
    }];
}





@end
